﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CNdotnet.Ezsocio.Core.Service;
using CNdotnet.Ezsocio.EmailService;
using CNdotnet.Ezsocio.TemplateEngine;

#endregion

namespace CNdotnet.Ezsocio.EmailService
{
    /// <summary>
    /// The service of email
    /// </summary>
    public class MailHandler : ServiceBase
    {
        #region Public Methods

        /// <summary>
        /// Sends the specified mail.
        /// </summary>
        /// <param name="mail">The mail.</param>
        public void Send(MailContract mail)
        {
            MailWaitItem waitItem = mail.ConvertToEntity();
            MailEventArgs arg = new MailEventArgs(waitItem.Content, SentAction.Waiting);
            OnBeforeEmailSent(waitItem, arg);
            waitItem.Content = arg.Body;

            try
            {
                LoadRepository<IMailRepository>().Save(waitItem);
                OnAfterEmailSent(waitItem, arg);
            }
            catch(Exception ex)
            {
                arg.Error = ex;
                OnEmailSentFailed(waitItem, arg);
            }
        }

        /// <summary>
        /// Sends the specified mails.
        /// </summary>
        /// <param name="mails">The mails.</param>
        public void Send(List<MailContract> mails)
        {
            foreach (MailContract mail in mails)
            {
                Send(mail);
            }
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs before email sent
        /// </summary>
        public static event EventHandler<MailEventArgs> BeforeEmailSent;

        /// <summary>
        /// Occures after email sent
        /// </summary>
        public static event EventHandler<MailEventArgs> AfterEmailSent;

        /// <summary>
        /// Occurs when email failed
        /// </summary>
        public static event EventHandler<MailEventArgs> EmailSentFailed;

        #endregion

        #region Protected Virtual Methods

        /// <summary>
        /// Called when email sent.
        /// </summary>
        /// <param name="mail">The mail.</param>
        protected static void OnBeforeEmailSent(Object mail, MailEventArgs e)
        {
            EventHandler<MailEventArgs> tempHandler = BeforeEmailSent;
            if (tempHandler != null)
            {
                tempHandler(mail, e);
            }
        }

        protected static void OnAfterEmailSent(Object mail, MailEventArgs e)
        {
            EventHandler<MailEventArgs> tempHandler = AfterEmailSent;
            if (tempHandler != null)
            {
                tempHandler(mail, e);
            }
        }

        /// <summary>
        /// Called when email failed.
        /// </summary>
        /// <param name="mail">The mail.</param>
        protected static void OnEmailSentFailed(Object mail, MailEventArgs e)
        {
            EventHandler<MailEventArgs> tempHandler = EmailSentFailed;
            if (tempHandler != null)
            {
                tempHandler(mail, e);
            }
        }

        #endregion
    }
}
